import { ElMessage } from "element-plus";
import Nifi from "../../nifi";
import ListEl from "./list.vue"
import { h } from "vue";

export default (nifi: Nifi) => {

    // 数据数据溯源列表数据
    const getList = async (params: { maxResult?: number, incrementalResults?: boolean, summarize?: boolean, searchTerms?: any, minimumFileSize?: string } = {}) => {
        const { incrementalResults = false, summarize = true } = params;
        const data = { provenance: { request: params } };
        let url = `/nifi-api/provenance`
        let res: any = undefined
        try {
            res = await nifi.api.post(url, data)
            res = res.provenance;
            while (res.finished == false) {
                await new Promise((resolve) => setTimeout(() => resolve(true), 500));
                url = `${url}/${res.id}?summarize=${summarize}&incrementalResults=${incrementalResults}`
                res = await nifi.api.get(url)
                res = res.provenance;
            }
            if (res.id) {
                await nifi.api.delete(`${url}/${res.id}`)
            }
            res = res.results;
        } catch (error) {
            console.error(error)
            ElMessage.error('获取数据失败 :: ' + error)
        }
        return res;
    }

    // detal
    const detail = async (id:string) => {
        const url = '/nifi-api/provenance-events/' + id;
        const res = await nifi.api.get(url);
        //
        return res.provenanceEvent;
    }

    // 重放
    const replayEvent = async (id:string) => {
        const url = `/nifi-api/provenance-events/replays`
        const data = { eventId: id }
        const res = await nifi.api.post(url,data);
        return res.provenanceEvent
    }

    // 查看内容数据
    const viewContent = async (id: string, type: 'input' | 'output') => {
        const url = `/nifi-api/provenance-events/${id}/content/${type}`
        const res = await nifi.api.get(url)
        return res;
    }
    // 下载文件
    const downContent = (id: string, type: 'input' | 'output', name?: string) => {
        const url = `/nifi-api/provenance-events/${id}/content/${type}`
        const n = name || `${type}.json`
        nifi.api.download(url,n)
    }

    // 获取流程图
    const flowFileGraph = async (uuid:string) => {
        const data = { lineage: { request: {lineageRequestType: "FLOWFILE",uuid} } };
        let url = `/nifi-api/provenance/lineage`
        let res: any = undefined
        try {
            res = await nifi.api.post(url, data)
            res = res.lineage;
            if (res.id) {
                await nifi.api.delete(`${url}/${res.id}`)
            }
            res = res.results;
        } catch (error) {
            console.error(error)
            ElMessage.error('获取数据失败 :: ' + error)
        }
        return res;
    }
    //


    const open = (id?:string,uuid?:string) => {
        return nifi.box.dialog((box) => h(ListEl,{nifi,uuid,id,onClose:() => box.close()}),{title:"数据溯源",showFooter:false})
    }

    return {
        getList,
        detail,
        replayEvent,
        viewContent,
        downContent,
        flowFileGraph,
        open
    }
}